home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*\
- | UIPropSh.cpp - DevUI(tm) property sheet class |
- | From Developing User Interfaces for Microsoft Windows |
- | Copyright (c) 1999, Everett N. McKay |
- \*---------------------------------------------------------------------------*/
-
- #include "stdafx.h"
- #include "DevUI.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
- // class CMcPropertySheet
-
- IMPLEMENT_DYNAMIC(CMcPropertySheet, CPropertySheet)
-
- BEGIN_MESSAGE_MAP (CMcPropertySheet, CPropertySheet)
- END_MESSAGE_MAP ()
-
- CMcPropertySheet::CMcPropertySheet() :
- CPropertySheet()
- {
- m_RemoveApply = m_RemoveCancel = FALSE;
- m_pButtonList = 0;
- }
-
- CMcPropertySheet::CMcPropertySheet (UINT titleID, CWnd *pParent,
- UINT selectPage) :
- CPropertySheet(titleID, pParent, selectPage)
- {
- m_RemoveApply = m_RemoveCancel = FALSE;
- m_pButtonList = 0;
- }
-
- CMcPropertySheet::CMcPropertySheet (LPCTSTR titleText, CWnd *pParent,
- UINT selectPage) :
- CPropertySheet (titleText, pParent, selectPage)
- {
- m_RemoveApply = m_RemoveCancel = FALSE;
- m_pButtonList = 0;
- }
-
- // effect: Initializes the property sheet by creating and moving buttons as necessary.
- // Note that this code handles the Help button (obtained with
- // m_psh.dwFlags |= PSH_HASHELP;), since all calculations are relative to the
- // Apply button and the right margin (which includes the Help button if it is
- // there.) However, this code does not work with the PSH_NOAPPLYNOW flag, again
- // since calculations are relative to the Apply button and will be wrong if the
- // Apply button has been removed.
- BOOL CMcPropertySheet::OnInitDialog ()
- {
- CWnd *pOK, *pCancel, *pApply;
- CRect cancelRect, applyRect;
- BOOL retVal;
-
- // call base class
- retVal = CPropertySheet::OnInitDialog();
-
- // gather info
- if ((pOK = GetDlgItem(IDOK)) == 0 ||
- (pCancel = GetDlgItem(IDCANCEL)) == 0 ||
- (pApply = GetDlgItem(ID_APPLY_NOW)) == 0)
- return retVal;
- ASSERT_VALID(pOK);
- ASSERT_VALID(pCancel);
- ASSERT_VALID(pApply);
- pApply->GetWindowRect(&applyRect);
- ScreenToClient(&applyRect);
- pCancel->GetWindowRect(&cancelRect);
- ScreenToClient(&cancelRect);
-
- // deal with button changes
- if (m_pButtonList)
- {
- CWnd *pLastButton = 0;
- CRect psRect;
- int i, newButtonNumber, buttonCount = 0, buttonOffset, buttonSpaceWidth,
- dialogWidth, rightMarginWidth, buttonWidth, buttonHeight;
- BOOL cancelUsed = FALSE, applyUsed = FALSE;
-
- // set sizes
- GetClientRect(&psRect);
- dialogWidth = psRect.Width();
- buttonWidth = cancelRect.Width();
- buttonHeight = cancelRect.Height();
- if ((rightMarginWidth = dialogWidth - applyRect.left - buttonWidth) <= 0)
- rightMarginWidth = 6; // default value (4 du in small fonts mode)
- if ((buttonSpaceWidth = applyRect.left - cancelRect.left - buttonWidth) <= 0)
- buttonSpaceWidth = 6; // default value (4 du in small fonts mode)
-
- // create new buttons
- for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
- {
- if (m_pButtonList[i].ID == -1)
- break;
- buttonCount++;
-
- // handle each button, create new buttons
- if (m_pButtonList[i].ID == IDOK)
- {
- pOK->SetWindowText(m_pButtonList[i].Name); // rename - may be different
-
- // clear the default style if button doesn't have focus
- if (!m_pButtonList[i].IsDefault)
- {
- LONG style = pOK->GetStyle();
- style &= ~BS_DEFPUSHBUTTON;
- SetWindowLong(pOK->m_hWnd, GWL_STYLE, style);
- }
- }
- else if (m_pButtonList[i].ID == IDCANCEL)
- cancelUsed = TRUE;
- else if (m_pButtonList[i].ID == ID_APPLY_NOW)
- applyUsed = TRUE;
- else
- {
- // create a new button
- m_Buttons[newButtonNumber].Create(m_pButtonList[i].Name, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
- CRect(0, 0, buttonWidth, buttonHeight), pOK->GetParent(), m_pButtonList[i].ID);
- m_Buttons[newButtonNumber].SetFont(pOK->GetFont());
- newButtonNumber++;
- }
- }
-
- // move buttons
- buttonOffset = dialogWidth - (rightMarginWidth + buttonCount * buttonWidth + (buttonCount - 1) * buttonSpaceWidth);
- for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
- {
- if (m_pButtonList[i].ID == -1)
- break;
-
- // move each button
- if (m_pButtonList[i].ID == IDOK)
- {
- pOK->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
- pLastButton = pOK;
- }
- else if (m_pButtonList[i].ID == IDCANCEL)
- {
- pCancel->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
- pLastButton = pCancel;
- }
- else if (m_pButtonList[i].ID == ID_APPLY_NOW)
- {
- pApply->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
- pLastButton = pApply;
- }
- else
- {
- // set tab order of new buttons
- m_Buttons[newButtonNumber].SetWindowPos(pLastButton, buttonOffset, cancelRect.top, buttonWidth, buttonHeight, SWP_NOACTIVATE);
- pLastButton = &m_Buttons[newButtonNumber++];
- }
- buttonOffset += buttonWidth + buttonSpaceWidth;
- }
-
- // hide and disable unused buttons
- if (!cancelUsed)
- {
- pCancel->ShowWindow(SW_HIDE);
- pCancel->EnableWindow(FALSE);
- }
- if (!applyUsed)
- {
- pApply->ShowWindow(SW_HIDE);
- pApply->EnableWindow(FALSE);
- }
-
- // set the input focus
- for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
- {
- if (m_pButtonList[i].ID == -1)
- break;
-
- if (m_pButtonList[i].ID == IDOK)
- {
- if (m_pButtonList[i].IsDefault)
- {
- pOK->SetFocus();
-
- LONG style = pOK->GetStyle();
- style |= BS_DEFPUSHBUTTON;
- SetWindowLong(pOK->m_hWnd, GWL_STYLE, style);
- break;
- }
- }
- else if (m_pButtonList[i].ID == IDCANCEL)
- {
- if (m_pButtonList[i].IsDefault)
- {
- pCancel->SetFocus();
-
- LONG style = pCancel->GetStyle();
- style |= BS_DEFPUSHBUTTON;
- SetWindowLong(pCancel->m_hWnd, GWL_STYLE, style);
- break;
- }
- }
- else if (m_pButtonList[i].ID == ID_APPLY_NOW)
- {
- if (m_pButtonList[i].IsDefault)
- {
- pApply->SetFocus();
-
- LONG style = pApply->GetStyle();
- style |= BS_DEFPUSHBUTTON;
- SetWindowLong(pApply->m_hWnd, GWL_STYLE, style);
- break;
- }
- }
- else
- {
- pLastButton = &m_Buttons[newButtonNumber++];
- if (m_pButtonList[i].IsDefault)
- {
- pLastButton->SetFocus();
-
- LONG style = pLastButton->GetStyle();
- style |= BS_DEFPUSHBUTTON;
- SetWindowLong(pLastButton->m_hWnd, GWL_STYLE, style);
- break;
- }
- }
- }
-
- return 0; // return 0 to preserve current input focus
- }
- else if (m_RemoveApply || m_RemoveCancel)
- {
- // handle Apply
- if (m_RemoveApply)
- {
- pApply->ShowWindow(SW_HIDE);
- pApply->EnableWindow(FALSE);
- }
-
- // handle Cancel
- if (m_RemoveCancel)
- {
- pCancel->ShowWindow(SW_HIDE);
- pCancel->EnableWindow(FALSE);
- }
- else if (m_RemoveApply)
- {
- pCancel->MoveWindow(&applyRect, FALSE);
- }
-
- // handle OK
- if (m_RemoveApply && m_RemoveCancel)
- pOK->MoveWindow(&applyRect, FALSE);
- else
- pOK->MoveWindow(&cancelRect, FALSE);
- }
-
- return retVal;
- }
-
-